﻿Shader "SongShaderDemo/LambertLigth"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "LigthMode"="ForwarBase"}
        LOD 100
		//逐片元兰伯特
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
    

            #include "UnityCG.cginc"
			#include "Lighting.cginc "

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
				float3 normal: NORMAL;//存储法线的盒子
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
				float3 wNormal:TEXCOORD2;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				o.wNormal = UnityObjectToWorldNormal(v.normal);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
				//_WorldSpaceLightPos0 unity提供的灯光向量
				float3 L = normalize(_WorldSpaceLightPos0);
				float3 N = normalize(i.wNormal);
				float halfLam = dot(L, N)*0.5 + 0.5;
				//_LightColor0.rgb unity提供的灯光强度
				float3 diffLight = halfLam * _LightColor0.rgb;
				fixed4 diffColor = fixed4(diffLight.rgb, 1);
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                return col* diffColor;
            }
            ENDCG
        }
		//逐顶点兰伯特
		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag


			#include "UnityCG.cginc"
			#include "Lighting.cginc "

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float3 normal: NORMAL;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
				//float3 wNormal:TEXCOORD2;
				fixed4 LightColor : TEXCOORD1;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;

			v2f vert(appdata v)
			{
				v2f o;
				v.vertex += float4(0.03,0,0,0);
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				//o.wNormal = UnityObjectToWorldNormal(v.normal);
				//_WorldSpaceLightPos0 unity提供的灯光向量
				float3 L = normalize(_WorldSpaceLightPos0);
				float3 N = normalize(UnityObjectToWorldNormal(v.normal));
				float halfLam = dot(L, N)*0.5 + 0.5;
				//_LightColor0.rgb unity提供的灯光强度
				float3 diffLight = halfLam * _LightColor0.rgb;
				fixed4 diffColor = fixed4(diffLight.rgb, 1);
				o.LightColor = diffColor;
				return o;
			}

			fixed4 frag(v2f i) : SV_Target
			{
				fixed4 cola = fixed4(1,1,0,1);
				fixed4 col = tex2D(_MainTex, i.uv);
				return col * i.LightColor;
			}
			ENDCG
		}

    }
}
